package com.dragon.quartz.config;

import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import javax.sql.DataSource;
import java.util.Properties;

/**
 * 定时任务配置（单机部署建议删除此类和qrtz数据库表，默认走内存会最高效）
 *
 * @author dragon
 * @version 1.0
 * @date 2024/9/5 17:45
 */
// @Configuration
public class ScheduleConfig {

    @Bean
    public DataSource quartzDataSource() {
        return DataSourceBuilder.create()
            .url("jdbc:mysql://127.0.0.1:3306/quartz_jobs?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8")
            .username("root")
            .password("root")
            .driverClassName("com.mysql.cj.jdbc.Driver")
            .build();
    }

    @Bean
    public SchedulerFactoryBean schedulerFactoryBean(DataSource dataSource) {
        SchedulerFactoryBean factory = new SchedulerFactoryBean();
        // 默认使用父项目数据库连接
        // factory.setDataSource(dataSource);
        factory.setDataSource(quartzDataSource());

        // quartz参数
        Properties prop = new Properties();
        // 实例名称（默认为quartzScheduler）
        prop.put("org.quartz.scheduler.instanceName", "dragonScheduler");
        // 实例节点 ID 自动生成
        prop.put("org.quartz.scheduler.instanceId", "AUTO");
        /* 线程池配置 */
        prop.put("org.quartz.threadPool.class", "org.quartz.simpl.SimpleThreadPool");
        prop.put("org.quartz.threadPool.threadCount", "20");
        prop.put("org.quartz.threadPool.threadPriority", "5");
        /* JobStore配置 */
        prop.put("org.quartz.jobStore.class", "org.springframework.scheduling.quartz.LocalDataSourceJobStore");
        /* 集群配置 */
        prop.put("org.quartz.jobStore.isClustered", "true");
        prop.put("org.quartz.jobStore.clusterCheckinInterval", "15000");
        prop.put("org.quartz.jobStore.maxMisfiresToHandleAtATime", "10");
        prop.put("org.quartz.jobStore.txIsolationLevelSerializable", "true");
        // 存储内容使用的类
        // prop.put("org.quartz.jobStore.dataSource", "quartzDataSource");
        // prop.put("org.quartz.jobStore.class", "org.springframework.scheduling.quartz.LocalDataSourceJobStore");
        // prop.put("org.quartz.dataSource.quartzDataSource.driver", "com.mysql.cj.jdbc.Driver");
        // prop.put("org.quartz.dataSource.quartzDataSource.URL", "jdbc:mysql://176.166.1.47:3306/quartz_jobs?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8");
        // prop.put("org.quartz.dataSource.quartzDataSource.user", "jinbi");
        // prop.put("org.quartz.dataSource.quartzDataSource.password", "gzusys");

        factory.setQuartzProperties(prop);

        factory.setSchedulerName("dragonScheduler");
        // 延时启动
        factory.setStartupDelay(1);
        factory.setApplicationContextSchedulerContextKey("applicationContextKey");
        // 可选，QuartzScheduler
        // 启动时更新己存在的Job，这样就不用每次修改targetObject后删除qrtz_job_details表对应记录了
        factory.setOverwriteExistingJobs(true);
        // 设置自动启动，默认为true
        factory.setAutoStartup(true);

        return factory;
    }

}
